package com.utils.commons;

import java.util.Random;


public class RandomUtils {

		 
    /** 自定义进制(没有0,1,o,l) */
    private static final char[] r = new char[]{'q', 'i', 'u', 'f', 'r', 'x', '3', 'w', 'e', '8', 'a', 'z', '5', 'c', '7', 'p', '4', 'v', 's', '2', 'k', '9', 'm', 'j', 'd', 'y', 't', 'n', '6', 'b', 'g', 'h'};
 
    /** 进制长度  r.length*/
    private static final int binLen = 32;
    
    /**
     * 时间截取长度
     */
    public static final int TIMESUBSTRING = 4;
 
    /**
     * 根据时间生成六位随机码
     * @param timeSubString 时间截取位置
     * @return
     */
    public static synchronized String toSerialCode(int timeSubString) {
		try {
			Thread.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		long id =  Long.parseLong((System.currentTimeMillis()+"").substring(timeSubString));
        char[] buf=new char[32];
        int charPos=32;
 
        while((id / binLen) > 0) {
            int ind=(int)(id % binLen);
            // System.out.println(num + "-->" + ind);
            buf[--charPos]=r[ind];
            id /= binLen;
        }
        buf[--charPos]=r[(int)(id % binLen)];
        // System.out.println(num + "-->" + num % binLen);
        String str=new String(buf, charPos, (32 - charPos));
        System.out.println("Random Code : "+str);
        return str;
    }
 
    /**
     * 随机数
     * @param from
     * @param to
     * @return
     */
    public static int randomInt(int from, int to) {
        Random r = new Random();
        return from + r.nextInt(to - from);
    }
    
    /**
     * 
     * @param charCount
     * @return
     */
    public static String getRandNum(int charCount) {
        String charValue = "";
        for (int i = 0; i < charCount; i++) {
            char c = (char) (randomInt(0, 10) + '0');
            charValue += String.valueOf(c);
        }
        return charValue;
    }
    
}
